package com.oocl.task0724.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.task0724.dao.UserDao;
import com.oocl.task0724.dao.impl.UserDaoImpl;
import com.oocl.task0724.pojo.User;

/**
 * Servlet implementation class UserImageServlet
 */
public class UserImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	private UserDao userDao=new UserDaoImpl(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		try{
//			InputStream in = userDao.getImage( Integer.parseInt(id) );
			
			User user = userDao.get( Integer.parseInt(id) );
			byte[] headImg = user.getHeadImg();
			OutputStream out = response.getOutputStream();
			
			response.setHeader("Content-Type", "image/jpeg");
			
			out.write(headImg);
			
			out.close();
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
	}

}
